package com.example.gzysmartcity;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.repository.model.City;

public class MyLocationListener extends BDAbstractLocationListener {
    private static final MutableLiveData<City> liveData = new MutableLiveData<>();

    @Override
    public void onReceiveLocation(BDLocation location) {
        //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
        //以下只列举部分获取地址相关的结果信息
        //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
//        String addr = location.getAddrStr();    //获取详细地址信息
//        String country = location.getCountry();    //获取国家
//        String district = location.getDistrict();    //获取区县
//        String street = location.getStreet();    //获取街道信息
//        String town = location.getTown();    //获取乡镇信息
        Log.i("TAG", "onReceiveLocation: " + location.getAddrStr());
        if (location.getAddrStr() == null) {
            liveData.setValue(new City("定位失败", "定位失败", "定位失败"));
            ToastUtils.showShort("请确定是否开启定位");
        } else {
            liveData.setValue(new City(location.getAdCode(), location.getCity(), location.getProvince()));
        }
        liveData.setValue(null);

    }

    public static MutableLiveData<City> getLiveData() {
        return liveData;
    }
}